<template>
  <div class="page">
    <el-form :inline="true">
      <el-form-item>
        <el-button type="primary" icon="el-icon-plus" @click="initForm"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <el-table :data="list" border>
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="分类名称" prop="catename"></el-table-column>
      <el-table-column label="分类描述" prop="sketch"></el-table-column>
      <el-table-column label="分类类型" prop="type">
        <template slot-scope="scope">
          <span>{{ scope.row.type == 1 ? "Tab型" : "菜单型" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" prop="图片">
        <template slot-scope="scope">
          <img :src="scope.row.image" />
        </template>
      </el-table-column>
      <el-table-column label="操作" prop="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="edit(scope.row)">编辑</el-button>
          <el-button type="danger" @click="remove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="paganation">
      <el-pagination
        @current-change="handlePageChange"
        :current-page="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 新增 -->
    <el-dialog title="帮助中心分类" :visible.sync="formDialogShow">
      <el-form :model="form" ref="ruleForm" :rules="rules">
        <el-form-item label="分类名称" label-width="80px" prop="catename">
          <el-input v-model="form.catename" autocomplete="off" @input="change($event)"></el-input>
        </el-form-item>
        <el-form-item label="分类描述" label-width="80px" prop="sketch">
          <el-input v-model="form.sketch" autocomplete="off" @input="change($event)"></el-input>
        </el-form-item>
        <el-form-item label="分类类型" label-width="80px" prop="type" >
          <el-select v-model="form.type">
            <el-option :value="0" label="菜单型"></el-option>
            <el-option :value="1" label="Tab型"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="图片" label-width="80px" prop="image">
          <ImageUpload v-model="form.image"></ImageUpload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit"> 确 定 </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  helpCateList,
  addHelpCate,
  removeHelpCate,
  editHelpCate,
} from "../../../api/ds/helpCenter";
import ImageUpload from "@/components/ImageUpload/index";

export default {
  components: {
    ImageUpload,
  },
  props: {},
  data() {
    return {
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      form: {
        catename: "",
        image: "",
        sketch: "",
        type: 0,
      },
      rules: {
        catename: [
          { required: true, message: "请输入类目名称", trigger: "blur" },
        ],
        sketch: [
          { required: true, message: "请输入类目简述", trigger: "blur" },
        ],
      },

      formDialogShow: false,
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getData();
  },
  methods: {
    change(e){
      this.$forceUpdate(e)
    },
    async getData() {
      let res = await helpCateList(this.queryParams);
      if (res.code === 200) {
        this.list = res.rows;
        this.total = res.total;
      }
    },
    remove(row) {
      // console.log(row);
      const ids = row.id;
      this.$confirm('是否确认删除轮播图编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return removeHelpCate(ids);
        })
        .then(() => {
          this.getData();
          this.msgSuccess("删除成功");
        });
    },
    edit(row) {
      console.log(row);
      this.form.catename = row.catename;
      this.form.sketch = row.sketch;
      this.form.type = row.type;
      this.form.image = row.image;
      this.form.id = row.id;

      this.formDialogShow = true;
    },

    handlePageChange(pageNum) {
      this.queryParams.pageNum = pageNum;
      this.getData();
    },

    initForm() {
      this.form = { catename: "", image: "", sketch: "", type: 0 };
      this.formDialogShow = true;
    },

    async submit() {
      this.$refs.ruleForm.validate(async (valid) => {
        console.log(valid);
        if (valid) {
          let res;
          console.log(this.form);
          if (this.form.id) {
            res = await editHelpCate(this.form);
            console.log(res);
          } else {
            res = await addHelpCate(this.form);
          }
          if (res.code === 200) {
            this.formDialogShow = false;
            this.queryParams.pageNum = 1;
            this.getData();

            // 清理表单
            this.form = {
              helpId: "",
              title: "",
              content: "",
              isCommon: "",
              id: "",
            };
          }
        } else {
          return false;
        }
      });
    },
  },
  mounted() {},
  beforeCreate() {},
  beforeMount() {},
  beforeUpdate() {},
  updated() {},
  beforeDestroy() {},
  destroyed() {},
  activated() {},
  deactivated() {},
};
</script>
<style lang="scss" scoped>
.page {
  width: 100%;
  box-sizing: border-box;
  padding: 20px;

  .paganation {
    width: 100%;
    height: 50px;
    display: flex;
    flex-flow: row nowrap;
    justify-content: flex-start;
    align-items: center;

    box-sizing: border-box;
    padding-top: 0 25px;
  }
}
</style>
